package com.ht.base.system.biz.manager.impl;

import com.ht.base.system.biz.convert.SysUserRoleBeanUtil;
import com.ht.base.system.biz.manager.SysUserRoleManager;
import com.ht.base.system.common.pojo.bo.SysUserRoleBO;
import com.ht.base.system.dao.dao.SysUserRoleDao;
import com.ht.base.system.dao.domain.SysUserRoleDO;
import org.base.common.appexception.AppException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author hdengxian
 * @Date 2023/5/22 9:27
 */
@Component
public class SysUserRoleManagerImpl implements SysUserRoleManager {
    @Resource
    private SysUserRoleDao sysUserRoleDao;
    @Override
    public Boolean addUserRoleRelation(SysUserRoleBO relation) throws AppException {
        if (relation == null) {
            return false;
        }
        if (relation.getSysUserId() == null || relation.getSysRoleId() == null) {
            return false;
        }
        SysUserRoleDO old = sysUserRoleDao.findByUserId(relation.getSysUserId());
        relation.setUpdateTime(new Date());
        if (old == null || old.getId() == null) {
            sysUserRoleDao.insert(SysUserRoleBeanUtil.sysUserRoleBOToDO(relation));
        } else {
            SysUserRoleDO userRoleDO = SysUserRoleBeanUtil.sysUserRoleBOToDO(relation);
            userRoleDO.setId(old.getId());
            sysUserRoleDao.updateById(userRoleDO);
        }

        return true;
    }

    @Override
    public SysUserRoleBO findByUserId(Long sysUserId) {
        return SysUserRoleBeanUtil.sysUserRoleDOToBO(sysUserRoleDao.findByUserId(sysUserId));
    }

    @Override
    public Boolean delRelation() {
        return null;
    }
}
